/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.home.map;

import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class Users {
	
	public static Vector<User> parse(InputSource xml) {
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp;
			sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			UsersHandler handler = new UsersHandler();
			xr.setContentHandler(handler);
			xr.parse(xml);
			return handler.getUsers();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/*
	 * Classe para processar o arquivo XML.
	 * Referência: http://www.ibm.com/developerworks/opensource/library/x-android/
	 * */
	private static class UsersHandler extends DefaultHandler {
		private StringBuilder builder;
		private User currentUser;
		private Vector<User> users;
		
		public Vector<User> getUsers() {
			return users;
		}
		
		@Override
		public void startDocument() throws SAXException {
			super.startDocument();
			builder = new StringBuilder();
			users = new Vector<User>();
		}
		
		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			super.startElement(uri, localName, qName, attributes);
			if (localName.equalsIgnoreCase("Member")) {
				currentUser = new User();
			}
			builder.setLength(0);
		}
		
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			super.characters(ch, start, length);
			builder.append(ch, start, length);
		}
		
		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			super.endElement(uri, localName, qName);
			if (currentUser != null) {
				String content = builder.toString().trim();
				if (localName.equalsIgnoreCase("Member")) {
					users.add(currentUser);
					currentUser = null;
				} else if (content.length() > 0) {
					Log.d("UsersHandler.endElement", localName + ": " + content);
					if (localName.equalsIgnoreCase("id")) {
						try {
							currentUser.id = Integer.parseInt(content);
						} catch (Exception e) {
							currentUser = null;
						}
					} else if (localName.equalsIgnoreCase("name")) {
						currentUser.name = content;
					} else if (localName.equalsIgnoreCase("position")) {
						try {
							double latitude, longitude;
							String[] values = content.split(":");
							latitude = Double.parseDouble(values[0]);
							longitude = Double.parseDouble(values[1]);
							currentUser.setLocation(latitude, longitude);
						} catch (NumberFormatException e) {	}
					}
				}
			}
		}
	}
}
